package com.barunsw.bwf.ems.neoarena.ne.pct.vop;

import java.util.Vector;


public class VopControl
{
	final int PACKET_SIZE = 2048;
	final int ERROR_LENGTH = 128;
	
	private int seqSize;
	private java.io.DataInputStream stream = null;
	
	public VopControl(Vector vecData) {
		int idx = 0;
		try {
    		byte[] newData = null;

			this.seqSize = vecData.size();
			
			if ( seqSize > 1 ) {
    			newData = new byte[seqSize * PACKET_SIZE];
			}
			else {
				char[] data = (char[])vecData.get(0);
    			newData = new byte[data.length];
			}

    		for ( int i = 0; i < seqSize; i++ ) {
				char[] data = (char[])vecData.get(i);
    			for ( int j = 0; j < PACKET_SIZE; j++ )
        			newData[idx++] = (byte)data[j];
			}
/*
    		for ( int i = 0; i < newData.length; i++ ) {
    			if ( i % 10 == 0 ) {
    				System.out.print("\n[" + i + "]\t");
    			}
    			
    			System.out.print(newData[i] + "\t");
    		}
*/    		
    		
    		stream = new java.io.DataInputStream(new java.io.ByteArrayInputStream(newData));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void printData(char[] data) {
		System.out.println("dataLength : " + data.length);
		for ( int i = 0; i < data.length; i++ ) {
			if ( (i%10) == 0 ) {
				System.out.println();
			}
			
			String hexStr = Integer.toHexString((int)data[i]);
			if ( hexStr.length() == 1 )
				hexStr = "0" + hexStr;
			System.out.print(" [" + hexStr + "]");
		}
		
		System.out.println();
	}
	
	public M3402_MODE[] getDisV5Ifc() {
		try {
			M3402_MODE[] mode = new M3402_MODE[seqSize];

			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);

				int cnt = stream.readShort();

				mode[i]  = new M3402_MODE();

				mode[i].ifCount = cnt;
				mode[i].totalcnt = stream.readShort();

				mode[i].ifId = new int[cnt];
				mode[i].varId = new int[cnt];
				mode[i].lnkcnt = new int[cnt];
				mode[i].portcnt = new int[cnt];
				mode[i].sts = new int[cnt];
				mode[i].name_ifc = new String[cnt];
System.out.println("ifCount : " + mode[i].ifCount + ", totalCount : " + mode[i].totalcnt);
				for ( int j = 0; j < cnt; j++ ) {
					mode[i].ifId[j] = stream.readInt();
					mode[i].varId[j] = stream.readShort();
					mode[i].lnkcnt[j] = stream.readShort();
					mode[i].portcnt[j] = stream.readShort();
					
System.out.println("ifId : " + mode[i].ifId[j] + ", varId : " + mode[i].varId[j] + ", linkcnt : " + mode[i].lnkcnt[j] + ", portcnt : " + mode[i].portcnt[j]);					
					
					mode[i].sts[j] = stream.readShort();

					stream.readShort();
					stream.readShort();
					
					byte[] name = new byte[M3402_MODE.MAX_NAME_IFC];
					stream.read(name, 0, name.length);
					mode[i].name_ifc[j] = (new String(name)).trim();
				}
			}

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				stream.close();
			}
			catch(Exception e1){}
		}
	}

	public M3408_MODE[] getDisV5Port() {
		M3408_MODE[] mode = new M3408_MODE[seqSize];

		try {
			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);
				
				int cnt = stream.readShort();

				mode[i]  = new M3408_MODE();
			
				stream.readShort();
				mode[i].portCount = cnt;

				mode[i].port = new int[cnt];
				mode[i].sts = new int[cnt];
				mode[i].ln = new int[cnt];
				mode[i].dn = new String[cnt];
			
				for ( int j = 0; j < cnt; j++ ) {
					mode[i].port[j] = stream.readShort();
					mode[i].sts[j] = stream.readShort();
					mode[i].ln[j] = stream.readInt();
					
					byte[] dn = new byte[M3408_MODE.MAX_DN];
					stream.read(dn, 0, dn.length);
					mode[i].dn[j] = (new String(dn)).trim();
				}
			}

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				stream.close();
			}
			catch(Exception e1){}
		}
	}

	public M3411_MODE getListVportBlk()
	{
		M3411_MODE mode = new M3411_MODE();

		try
		{
			mode.ifId = stream.readInt();
			mode.r_cnt = stream.readShort();
			mode.m_cnt = stream.readShort();

			for(int i=0; i<mode.MAX_PORT; i++)
				mode.r_port[i] = stream.readShort();

			for(int i=0; i<mode.MAX_PORT; i++)
				mode.m_port[i] = stream.readShort();

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3414_MODE[] getDisV5Var() {
		M3414_MODE[] mode = new M3414_MODE[seqSize];
		
		try {
			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);
				
				int cnt = stream.readShort();
				stream.readShort();
			
				mode[i]  = new M3414_MODE();
				mode[i].ifCount = cnt;

				mode[i].ifId = new int[cnt];
				mode[i].varId = new int[cnt];
				mode[i].sts = new int[cnt];
				mode[i].name_ifc = new String[cnt];
			
				for ( int j = 0; j < cnt; j++ ) {
					mode[i].ifId[j] = stream.readInt();
					mode[i].varId[j] = stream.readShort();
					mode[i].sts[j] = stream.readShort();
				
					byte[] name = new byte[M3414_MODE.MAX_NAME_IFC];
					stream.read(name, 0, name.length);
					mode[i].name_ifc[j] = (new String(name)).trim();
				}
			}

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				stream.close();
			}
			catch(Exception e1){}
		}
	}

	public M3421_MODE[] getDisV5Llnk() {
		M3421_MODE[] mode = new M3421_MODE[seqSize];

		try {
			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);
				
				int cnt = stream.readShort();
				stream.readShort();
			
				mode[i] = new M3421_MODE();
				mode[i].linkCount = cnt;

				mode[i].llnk = new int[cnt];
				mode[i].plnk = new int[cnt];
				mode[i].status = new int[cnt];
				mode[i].lcch15 = new String[cnt];
				mode[i].svc15 = new int[cnt];
				mode[i].lcch16 = new String[cnt];
				mode[i].svc16 = new int[cnt];
				mode[i].lcch31 = new String[cnt];
				mode[i].svc31 = new int[cnt];

				for ( int j = 0; j < cnt; j++ ) {
					mode[i].llnk[j] = stream.readShort();
					mode[i].plnk[j] = stream.readShort();
					mode[i].status[j] = stream.readInt();

					byte[] lcch15 = new byte[8];
					for (int k=0; k<8; k++) {
						lcch15[k] = stream.readByte();
					}

					mode[i].lcch15[j] = new String(lcch15);

					mode[i].svc15[j] = stream.readInt();

					byte[] lcch16 = new byte[8];
					for (int k=0; k<8; k++) {
						lcch16[k] = stream.readByte();
					}

					mode[i].lcch16[j] = new String(lcch16);
					mode[i].svc16[j] = stream.readInt();

					byte[] lcch31 = new byte[8];
					for (int k=0; k<8; k++) {
						lcch31[k] = stream.readByte();
					}

					mode[i].lcch31[j] = new String(lcch31);
					mode[i].svc31[j] = stream.readInt();
				}
			}
			
			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3425_MODE[] getDisV5Plnk()
	{
		M3425_MODE[] mode = new M3425_MODE[seqSize];
		
		try {
			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);
				
				int cnt = stream.readShort();
				stream.readShort();
			
				mode[i] = new M3425_MODE();
				mode[i].linkCount = cnt;
				mode[i].ifId = new int[cnt];
				mode[i].plnk = new int[cnt];
				mode[i].llnk = new int[cnt];
				mode[i].lnk_sts = new int[cnt];
				mode[i].ifName = new String[cnt];
				
				for ( int j = 0; j < cnt; j++ ) {
					mode[i].ifId[j] = stream.readInt();
					mode[i].plnk[j] = stream.readShort();
					mode[i].llnk[j] = stream.readShort();
					mode[i].lnk_sts[j] = stream.readShort();

					stream.readShort();

					byte[] name = new byte[M3425_MODE.MAX_NAME_IFC];
					stream.read(name, 0, name.length);
					mode[i].ifName[j] = (new String(name)).trim();
				}

				for ( int j = 0; j < M3425_MODE.MAX_TVIC_BD; j++ ) {
					mode[i].tvic_call_cnt[j] = stream.readShort();
					System.out.println("mode["+i+"].tvic_call_cnt["+j+"] = " + mode[i].tvic_call_cnt[j]);
					stream.readShort();
				}
			}
			
			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally {
			try {
				stream.close();
			}
			catch(Exception e1){}
		}
	}

	public M3426_MODE[] getDisV5Bch() {
		M3426_MODE[] mode = new M3426_MODE[M3426_MODE.MAX_LINK_PER_IF];

		try {
			int linkCount = stream.readShort();		// linkCount
//System.out.println(">>> linkCount : " + linkCount);			
			stream.readShort();		// dummy
			
			for ( int i = 0; i < M3426_MODE.MAX_LINK_PER_IF; i++ ) {
				mode[i] = new M3426_MODE();
				
				int llnk = -1;
				int lnk_sts = -1;
				int plnk = -1;
				int ib_map32 = -1;
				int[] port = new int[M3426_MODE.MAX_CH_PER_LINK];
				byte[] bch = new byte[M3426_MODE.MAX_CH_PER_LINK];
				
				if ( i < linkCount ) {
					llnk = stream.readShort();
					lnk_sts = stream.readShort();
					plnk = stream.readShort();
					stream.readShort();	// dummy
					ib_map32 = stream.readInt();
					
					for ( int j = 0; j < M3426_MODE.MAX_CH_PER_LINK; j++ )
						port[j] = stream.readShort();
					
					for ( int j = 0; j < M3426_MODE.MAX_CH_PER_LINK; j++ )
						bch[j] = stream.readByte();
				}
				else {
					for ( int j = 0; j < M3426_MODE.MAX_CH_PER_LINK; j++ )
						port[j] = -1;
					
					for ( int j = 0; j < M3426_MODE.MAX_CH_PER_LINK; j++ )
						bch[j] = -1;
				}

				mode[i].llnk = llnk;
				mode[i].lnk_sts = lnk_sts;
				mode[i].plnk = plnk;
				mode[i].ib_map32 = ib_map32;
				mode[i].port = port;
				mode[i].bch = bch;
/*				
System.out.println("(" + i + ")llnk:" + mode[i].llnk + ", lnk_sts:" + mode[i].lnk_sts + ", plnk:" + mode[i].plnk);
for ( int j = 0; j < M3426_MODE.MAX_CH_PER_LINK; j++ ) {
	System.out.print(bch[j] + " ");
}
System.out.println();
*/
			}

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3427_MODE[] getDisSeizeBch() {
		M3427_MODE[] mode = new M3427_MODE[seqSize];

		try {
			for ( int i = 0; i < seqSize; i++ ) {
				stream.reset();
				stream.skip(i*PACKET_SIZE);

				int cnt = stream.readShort();
				stream.readShort();

				mode[i] = new M3427_MODE();
				mode[i].cnt = cnt;
System.out.println("cnt : " + cnt);
				for ( int j = 0; j < mode[i].cnt; j++ ) {
					mode[i].id[j] = stream.readInt();
					mode[i].port[j] = stream.readShort();
					mode[i].bch[j] = stream.readShort();
				}
			}

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public M3430_MODE getDisV5Trc()
	{
		M3430_MODE mode = new M3430_MODE();

		try
		{
			mode.tot_cnt = stream.readShort();
			mode.ifc_cnt = stream.readShort();
			mode.port_cnt = stream.readShort();

			for(int i=0; i<7; i++)
			{
				mode.ifId[i] = stream.readInt();
				mode.port[i] = stream.readShort();
				mode.bch[i] = stream.readShort();
			}

			return mode;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public String getErrorReason()
	{
		byte[] err = new byte[ERROR_LENGTH];
		try
		{
			stream.read(err, 0, err.length);
			return (new String(err)).trim();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public int close()
	{
		try
		{
			stream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
